package com.xu.wemall.controller;

import com.alibaba.fastjson.JSONObject;
import com.xu.wemall.commons.utils.RedisTemplateUtil;
import com.xu.wemall.entry.User;
import com.xu.wemall.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

@Slf4j
@Api(tags = "Redis操作接口")
@RestController
@RequestMapping("/user")
public class RedisController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    @ApiOperation("查询用户详情")
    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Serializable id) {
        User user = userService.getById(id);
        redisTemplateUtil.set(user.getId(), user);
        User userObject = (User)redisTemplateUtil.get(user.getId());
        log.info(JSONObject.toJSONString(userObject));
        return userObject;
    }

}